package com.travelsmart.server.integration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.smart.hotelsmart.BillingAddressType;
import com.smart.hotelsmart.BookRoomType;
/*/# INICIO - EXERC. 1*/
import com.smart.hotelsmart.InfoBookHotelResponseType;
// FIM - EXERC. 1 #*/
import com.smart.hotelsmart.ListBookRoomType;
import com.smart.hotelsmart.PaymentType;
import com.travelsmart.client.bean.to.BandeiraCartaoCredito;
import com.travelsmart.client.bean.to.CartaoCreditoTO;
import com.travelsmart.client.bean.to.EnderecoPagamentoTO;
import com.travelsmart.client.bean.to.HotelTO;
import com.travelsmart.client.bean.to.QuartoTO;
import com.travelsmart.client.bean.to.ReservaHotelTO;
import com.travelsmart.client.bean.to.ReservaQuartoTO;
import com.travelsmart.server.util.UtilDate;

public class BuilderReservaHotelTO {
	
	//# INICIO - EXERC. 1
	public ReservaHotelTO criarReservaHotelTo(InfoBookHotelResponseType response) {
		assert response != null : " response = null";
		ReservaHotelTO reserva = new ReservaHotelTO();
		
		
		HotelTO hotel = new BuilderHotelTO().criarHotelTO(response.getHotel());
		reserva.setHotel(hotel);
		
		PaymentType payment = response.getPayment();
		CartaoCreditoTO cartao = criarCartao(payment);
		reserva.setCartao(cartao);
		
		BillingAddressType billingAddress = response.getBillingAddress();
		EnderecoPagamentoTO enderecoPagamento = criarEnderecoPagamento(billingAddress);
		reserva.setEnderecoPagamento(enderecoPagamento);
		
		reserva.setData(new Date());
		reserva.setDataEntrada(UtilDate.getDate(response.getArrival()));
		reserva.setDataSaida(UtilDate.getDate(response.getDeparture()));
		
		ListBookRoomType listBookRoom = response.getListBookRoom();
		List<BookRoomType> listRoom = listBookRoom.getBookRoom();
		List<ReservaQuartoTO> quartos = criarListReservaQuartoTO(listRoom, hotel.getListRoom());
		reserva.setQuartos(quartos);
		
		return reserva;
		
	}
	// FIM - EXERC. 1 #/
	
	private EnderecoPagamentoTO criarEnderecoPagamento(
			BillingAddressType billingAddress) {

		EnderecoPagamentoTO enderecoPagamento = new EnderecoPagamentoTO();
		enderecoPagamento.setCep(billingAddress.getZipCode());
		enderecoPagamento.setCidade(billingAddress.getCity());
		enderecoPagamento.setEndereco(billingAddress.getStreetAddress());
		enderecoPagamento.setEstado(billingAddress.getState());
		enderecoPagamento.setPais(billingAddress.getCountry());
		
		return enderecoPagamento;
	}

	private List<ReservaQuartoTO> criarListReservaQuartoTO(
			List<BookRoomType> listBookRoom, List<QuartoTO> listQuartosHotel ){
		
		List<ReservaQuartoTO> quartos = new ArrayList<ReservaQuartoTO>();
		
		for (BookRoomType bookRoomType : listBookRoom) {
			QuartoTO quarto = obterQuartoTO(bookRoomType.getCodeRoom(), listQuartosHotel);
			ReservaQuartoTO reservaQuartoTO = criarReservaQuartoTO(bookRoomType, quarto);
			quartos.add(reservaQuartoTO);
		}
		
		return quartos;
		
	}
	
		
	private ReservaQuartoTO criarReservaQuartoTO(BookRoomType bookRoomType,
			QuartoTO quarto) {
		
		ReservaQuartoTO reservaQuartoTO = new ReservaQuartoTO();
		reservaQuartoTO.setNome(bookRoomType.getFirstNameHost());
		reservaQuartoTO.setQtiAdultos(bookRoomType.getAmountAdults());
		reservaQuartoTO.setQtiCrianca(bookRoomType.getAmountChildren());
		reservaQuartoTO.setQuarto(quarto);
		reservaQuartoTO.setSobrenome(bookRoomType.getLastNameHost());
		
		return reservaQuartoTO;
		
	}

	private QuartoTO obterQuartoTO(int codQuarto, List<QuartoTO> listQuartosHotel) {
		
		QuartoTO retorno = null;
		for (QuartoTO quartoTO : listQuartosHotel) {
			
			if (quartoTO.getCodigoQuarto() == codQuarto) {
				retorno = quartoTO;
				break;
			}
			
		}
		assert retorno != null : "quarto = null p/ cod = " + codQuarto;
		return retorno;
	}

	private CartaoCreditoTO criarCartao(PaymentType payment) {
		CartaoCreditoTO cartao = new CartaoCreditoTO();
		
		
		//# INICIO - EXERC. 6
		cartao.setAnoVencimento(payment.getExpiryYear());
		cartao.setMesVencimento(payment.getExpiryMonth());
		cartao.setBandeira(BandeiraCartaoCredito.getEnum(payment.getCardType()));
		cartao.setCodigoSeguranca(payment.getSecurityCode());
		cartao.setNumero(payment.getCardNumber());
		//FIM - EXERC. 6 #/
		return cartao;
	}
	
	
	

}
